package aom.ju.ss.view.main

import android.content.Intent
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import aom.ju.ss.R
import aom.ju.ss.databinding.GuldeActivityBinding
import aom.ju.ss.utils.VG
import aom.ju.ss.view.base.BaseActivity
import java.util.*

class GuideActivity : BaseActivity() {
    private lateinit var binding: GuldeActivityBinding

    private val pics = intArrayOf(R.mipmap.guide_pic1, R.mipmap.guide_pic2)

    private val views: ArrayList<View> = arrayListOf()

    override fun init() {
        binding = viewBinding as GuldeActivityBinding

        val mParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        )

        for (i in pics) {
            val iv = ImageView(this)
            iv.scaleType = ImageView.ScaleType.CENTER_CROP
            iv.layoutParams = mParams
            iv.setImageResource(i)
            views.add(iv)
        }
        val view: View = layoutInflater.inflate(R.layout.guide_loading3, null, false)
        view.findViewById<View>(R.id.tv_start).setOnClickListener { //防止连击
            startUp()
        }
        views.add(view)
        binding.viewpager.adapter = ViewPagerAdapter(views)

        binding.tvInto.setOnClickListener {
            startUp()
        }

        binding.viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                binding.tvInto.VG(position != 2)
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })

        binding.myInd.setViewPager(binding.viewpager)
    }


    private fun startUp() {
        startActivity(Intent(activity, LoginActivity::class.java))
        finish()
    }

    override val lay: Int
        get() = R.layout.gulde_activity
}

class ViewPagerAdapter(val views: ArrayList<View>) : PagerAdapter() {
    override fun getCount(): Int {
        return views.size
    }

    override fun instantiateItem(view: View, position: Int): Any {
        (view as ViewPager).addView(views[position], 0)
        return views[position]
    }

    override fun isViewFromObject(view: View, arg1: Any): Boolean {
        return view === arg1
    }

    override fun destroyItem(view: View, position: Int, arg2: Any) {
        (view as ViewPager).removeView(views[position])
    }
}